home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 42 / Amiga Format AFCD42 (Issue 126, Aug 1999).iso / -serious- / programming / c / bitmap24 / zbitmap24.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-17  |  3.0 KB  |  127 lines

  1. /* -------------------------------------------------------------------------- *\
  2.    ZBITMAP24.CPP, a BitMap24 subclass which introduces simple Z-Buffering
  3.    Copyright (C) 1999  Jarno van der Linden
  4.    jarno@kcbbs.gen.nz
  5.  
  6.    This library is free software; you can redistribute it and/or
  7.    modify it under the terms of the GNU Library General Public
  8.    License as published by the Free Software Foundation; either
  9.    version 2 of the License, or (at your option) any later version.
  10.  
  11.    This library is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.    Library General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU Library General Public
  17.    License along with this library; if not, write to the
  18.    Free Software Foundation, Inc., 59 Temple Place - Suite 330, Cambridge,
  19.    MA 02139, USA.
  20.  
  21.  
  22.    01 May 1999: Brought it all together in some sort of distributable form
  23. \* -------------------------------------------------------------------------- */
  24.  
  25. /* -------------------------------- Includes -------------------------------- */
  26. #include <string.h>
  27.  
  28. #include "zbitmap24.h"
  29.  
  30. /* ------------------------------ Definitions ------------------------------- */
  31.  
  32. /* --------------------------------- Macros --------------------------------- */
  33.  
  34. /* -------------------------------- Typedefs -------------------------------- */
  35.  
  36. /* ------------------------------ Proto Types ------------------------------- */
  37.  
  38. /* -------------------------------- Structs --------------------------------- */
  39.  
  40. /* -------------------------------- Globals --------------------------------- */
  41.  
  42. /* ---------------------------------- Code ---------------------------------- */
  43. ZBitMap24::ZBitMap24()
  44.     : BitMap24()
  45. {
  46.     zbuffer = NULL;
  47. }
  48.  
  49.  
  50. ZBitMap24::~ZBitMap24()
  51. {
  52.     if(zbuffer)
  53.         delete[] zbuffer;
  54.     zbuffer = NULL;
  55. }
  56.  
  57.  
  58. char *ZBitMap24::GetErrorStr(int error)
  59. {
  60.     switch(error)
  61.     {
  62.         case ZBITMAP24_ERROR_NOZBUFFER:
  63.             return("Z-Buffer couldn't be allocated\n");
  64.     }
  65.  
  66.     return BitMap24::GetErrorStr(error);
  67. }
  68.  
  69.  
  70. char *ZBitMap24::GetErrorStr()
  71. {
  72.     return BitMap24::GetErrorStr();
  73. }
  74.  
  75.  
  76. void ZBitMap24::SetMaxZ(double maxz_arg)
  77. {
  78.     UWORD z;
  79.     UWORD *zb;
  80.  
  81.     maxz = maxz_arg;
  82.     if(maxz > 0.0)
  83.         mul = 65535.0/log(maxz+1.0);
  84.     else
  85.         mul = 0.0;
  86.  
  87.     z = GetBufferZ(maxz);
  88.     zb = zbuffer;
  89.     for(int t=GetWidthFast()*GetHeightFast()-1; t>=0;t--)
  90.         *(zb++) = z;
  91. }
  92.  
  93.  
  94. void ZBitMap24::SetColour(UBYTE r,UBYTE g,UBYTE b,double z,int x,int y)
  95. {
  96.     if(BoundsCheck(x,y))
  97.         SetColourFast(r,g,b,z,x,y);
  98. }
  99.  
  100.  
  101. void ZBitMap24::SetColour(const Colour &c,double z,int x,int y)
  102. {
  103.     if(BoundsCheck(x,y))
  104.         SetColourFast(c,z,x,y);
  105. }
  106.  
  107.  
  108. void ZBitMap24::SetSize(UWORD width_arg, UWORD height_arg)
  109. {
  110.     UWORD width,height;
  111.  
  112.     if(zbuffer)
  113.         delete[] zbuffer;
  114.     zbuffer = NULL;
  115.  
  116.     BitMap24::SetSize(width_arg,height_arg);
  117.  
  118.     width = GetWidthFast();
  119.     height = GetHeightFast();
  120.  
  121.     zbuffer = new UWORD[width*height];
  122.     if(zbuffer)
  123.         SetMaxZ(1.0);
  124.     else
  125.         SetError(ZBITMAP24_ERROR_NOZBUFFER);
  126. }
  127.